import { getProfileAPI } from '@/api/user'
import { routes, resetRouter } from '@/router/index'

export default {
  namespaced: true,
  state: () => {
    return {
      // 权限标识
      permission: [],
      // 路由规则
      menuList: []
    }
  },
  mutations: {
    // 设置权限标识
    setPromission (state, newPermission) {
      state.permission = newPermission
    },
    // 设置路由规则
    setMenuList (state, asyncRoutes) {
      state.menuList = [...routes, ...asyncRoutes]
    },
    // 清空路由规则
    clearMenuList (state) {
      state.menuList = []
      resetRouter()
    }
  },
  actions: {
    async getPromission (store) {
      const res = await getProfileAPI()
      store.commit('setPromission', res.data.permissions)
      return res.data.permissions
    }
  }
}
